#!/usr/bin/env python
# encoding: utf-8
# @author: liusir
# @file: common_api_info.py
# @time: 2021/6/30 8:35 下午

import os
import json
from common.config import Config
from common.log_utils import logger
from requests.exceptions import RequestException

current_path = os.path.dirname(os.path.abspath(__file__))
config_file_dir = os.path.join(current_path, '..', 'conf', 'config.ini')

class CommonApiInfo:
    def __init__(self,session):
        self.session = session
        self.hosts = Config(config_file_dir).HOSTS

    def get_access_token_api(self,grant_type,appid,secret):
        logger.info( '调用get_access_token接口执行' )
        url_params = {
            "grant_type":grant_type,
            "appid":appid,
            "secret":secret
        }
        try:
            response = self.session.get(url='https://%s/cgi-bin/token'%self.hosts,
                                    params = url_params)
        except RequestException as e:
            logger.error( '调用get_access_token接口执行出现request异常' )
            response = None
        except Exception as e:
            logger.error('调用get_access_token接口执行出现系统异常，原因是：%s'%e.__str__())
            response = None
        return response

    def create_user_tag_api(self,access_token,tag_name):
        url_params = {
            "access_token":access_token
        }
        json_data = json.loads('{"tag" : { "name" : "%s"} }'%tag_name)
        response = self.session.post(url='https://%s/cgi-bin/tags/create'%self.hosts,
                                     params = url_params,
                                     json = json_data)
        return response

    def get_all_user_tag_api(self,access_token):
        url_params = {
            "access_token": access_token
        }
        response = self.session.get(url='https://%s/cgi-bin/tags/get'%self.hosts,
                                    params = url_params)
        return response



